import {Image, ScrollView, Swiper, SwiperItem, Text, View} from '@tarojs/components';
import Taro from '@tarojs/taro';
import React, {Component} from 'react';
import * as T from '../types';
import './shoppage.less';
import actions from '../actions/index';
import {connect} from 'react-redux';
import {store2Props} from '../selectors';
import Blank from '@/pages/common/blank';
import notdataImg from '@/assets/image/none/notdata.png';

type IShopPageProps = T.IProps & T.IShopPageProps;
//@ts-ignore
@connect<Partial<IShopPageProps>, T.IShopPageState>(store2Props, actions)
export default class Info extends Component<Partial<IShopPageProps>, T.IShopPageState> {
  constructor(props: IShopPageProps) {
    super(props);
    this.state = {
      refresherTriggered: false,
      shopToken: '',
      shopUser: '',
    };
  }

  componentDidMount() {
    let shopUser = Taro.getStorageSync('b2b-wechat@login').shopUser || '';
    let shopToken = Taro.getStorageSync('b2b-wechat@login').shopToken || '';
    this.setState({
      shopUser,
      shopToken,
    });
  }

  _onScrollToLower = () => {
    let {
      main: {pageNum, totalPages},
      actions: {action},
      searchValue,
    } = this.props;
    let num = pageNum + 1;
    if (num == totalPages) return;
    let params = {
      keywords: searchValue,
      pageNum: num,
      pageSize: 10,
    };
    this.props.actions.action.commonChange('main.pageNum', num);
    this.props.actions.action.queryShop(params);
  };

  _onRefresherRefresh = () => {
    let {searchValue} = this.props;
    const {refresherTriggered} = this.state;
    if (refresherTriggered) return;
    this.setState({
      refresherTriggered: true,
    });
    let params = {
      keywords: searchValue,
      pageNum: 0,
      pageSize: 10,
    };
    this.props.actions.action.commonChange('main.pageNum', 0);
    this.props.actions.action.queryShop(params);
    setTimeout(() => {
      this.setState({
        refresherTriggered: false,
      });
    }, 2000);
  };

  render() {
    let itemStr = Taro.getStorageSync('b2b-wechat@login');
    let {refresherTriggered, shopToken, shopUser} = this.state;
    let {
      main: {shopList, pageNum, totalPages, isLoading},
      actions: {action},
    } = this.props;
    return (
      <ScrollView
        className="PageShopPage"
        scrollY
        onScrollToLower={this._onScrollToLower}
        refresherEnabled
        refresherTriggered={refresherTriggered}
        onRefresherPulling={this._onRefresherRefresh}
      >
        {shopList && shopList.length > 0 ? (
          <View>
            {shopList.map((item, index) => {
              return (
                <View
                  className="postViews"
                  key={index}
                  onClick={() => {
                    if (!itemStr) {
                      Taro.navigateTo({
                        url: '/pages/package-C/login/login/index',
                      });
                      return;
                    }
                    Taro.navigateTo({
                      url: `/pages/package-B/goods/goods-details/index?skuId=${item.id}`,
                    });
                  }}
                >
                  <View>
                    <Image className="searchShop_image" src={item.goodsInfo.goodsInfoImg} mode="widthFix"></Image>
                  </View>
                  <View className="i_right">
                    <Text className="search_shop_name">{item.goodsInfo.goodsInfoName}</Text>
                    <Text className="marketPrice">￥{item.goodsInfo.marketPrice}</Text>
                  </View>
                </View>
              );
            })}
          </View>
        ) : (
          <Blank
            content="没有搜索结果，换一个关键词试试吧~"
            img={notdataImg}
            imgStyle={{width: '400rpx', height: '400rpx'}}
            textStyle={{
              fontSize: '32rpx',
              color: '#999999',
              textAlign: 'center',
              marginTop: '40rpx',
            }}
          />
        )}
        {shopList.length != 0 && pageNum + 1 != totalPages && <View className="status">加载中</View>}
        {!isLoading && shopList.length != 0 && pageNum + 1 == totalPages && <View className="status">没有更多了</View>}
      </ScrollView>
    );
  }
}

//create by moon https://github.com/creasy2010/moon
